package com.cmcc.aregister.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cmcc.aregister.MyApplication;
import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.lazyimage.LazyImageView;
import com.cmcc.aregister.cs.model.Doctor;
import com.cmcc.aregister.cs.model.Schedule;
import com.cmcc.aregister.cs.util.Utils;

public class ChoiceDoctorAdapter extends BaseAdapter {
	private Context context = null;
	public List<Doctor> docList = null;
	public List<Schedule> schList = new ArrayList<Schedule>();
	private Handler handler;
	public int allPage;
	public int currPage;
	public boolean isLocal;
	private int size;

	public ChoiceDoctorAdapter(Context context, List<Doctor> list, int allPage,
			Handler handler, int currPage, boolean isLocal) {
		this.context = context;
		this.docList = list;
		this.handler = handler;
		this.allPage = allPage;
		this.currPage = currPage;
		this.isLocal = isLocal;
		size = docList.size();
		if (isLocal) {
			for (Doctor doc : list) {
				for (Schedule s : doc.schedule_list) {
					/*if (!"0".equals(s.left_num) && !"null".equals(s.left_num)) {
						schList.add(s);
					}*/
					schList.add(s);
				}
			}
			size = schList.size();
		}
	}

	public void setDate(List<Doctor> list, boolean isLocal) {
		this.docList = list;
		size = docList.size();
		schList.clear();
		if (isLocal) {
			for (Doctor doc : list) {
				for (Schedule s : doc.schedule_list) {
					/*if (!"0".equals(s.left_num) && !"null".equals(s.left_num)) {
						schList.add(s);
					}*/
					schList.add(s);
				}
			}
			size = schList.size();
		}
	}

	public void sequenceDate(boolean isByTime) {
		try {
			if (isByTime) {
				schList = Utils.sequenceDoctorListByName(schList);
			} else {
				schList = Utils.sequenceDoctorListByTime(schList);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public int getCount() {
		if (allPage == 0) {
			allPage = 1;
		}
		if (currPage == 0) {
			currPage = 1;
		}
		if (allPage > currPage) {
			return size + 1;
		} else {
			return size;
		}
	}

	@Override
	public boolean isEnabled(int position) {
		if (position >= size) {
			return false;
		} else {
			return true;
		}
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		if (position >= size) {
			handler.sendMessage(handler.obtainMessage(-1, position));
			convertView = LayoutInflater.from(context).inflate(
					R.layout.list_wait, null);
			convertView.setTag(null);
			convertView.setEnabled(false);
			return convertView;
		} else {
			ViewHolder holder;
			if (convertView != null && convertView.getTag() != null) {
				holder = (ViewHolder) convertView.getTag();
			} else {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.a_choice_hospital, null);
				holder.image = (LazyImageView) convertView
						.findViewById(R.id.image);
				holder.name = (TextView) convertView.findViewById(R.id.name);
				holder.address = (TextView) convertView
						.findViewById(R.id.address);
				holder.level = (TextView) convertView.findViewById(R.id.level);
				holder.one_icon_img=(ImageView)convertView.findViewById(R.id.one_icon);
				holder.two_icon_img=(ImageView)convertView.findViewById(R.id.two_icon);
				holder.yuyue_num=(TextView)convertView.findViewById(R.id.yuyue_num);
				holder.detail = (Button) convertView.findViewById(R.id.detail);
				holder.detail.setOnClickListener(onClickListener);
				holder.attention_layout = (LinearLayout) convertView
						.findViewById(R.id.attention_layout);
				convertView.setTag(holder);
			}
			if (isLocal) {
				getLocalView(holder, position);
			} else {
				getOtherView(holder, position);
			}

			return convertView;
		}
	}

	public void getLocalView(ViewHolder holder, int position) {
		Schedule sch = schList.get(position);
		Doctor doctor = sch.doctor;
		holder.one_icon_img.setBackgroundResource(R.drawable.c_time_icon);
		holder.two_icon_img.setBackgroundResource(R.drawable.c_dept_icon);
		holder.detail.setTag(position);
		holder.address.setText(doctor.dep_name);
		holder.image.loadImage(doctor.image, 0, 1);
		holder.name.setText(doctor.doctor_name);
		holder.level.setText(sch.to_date + " " + sch.time_type + "  " + context.getString(R.string.left_num));
		holder.yuyue_num.setText(sch.left_num);
		String is_attention = MyApplication.getInstance().doctorMap
				.get(doctor.doctor_id);
		if (is_attention == null || "".equals(is_attention)) {
			is_attention = doctor.is_attention;
		}
		if ("1".equals(is_attention)) {
			holder.attention_layout.setVisibility(View.VISIBLE);
		} else {
			holder.attention_layout.setVisibility(View.INVISIBLE);
		}
	}

	public void getOtherView(ViewHolder holder, int position) {
		Doctor doctor = docList.get(position);
		holder.detail.setTag(position);
		holder.image.loadImage(doctor.image, 0, 1);
		holder.address.setText(doctor.dep_name);
		holder.name.setText(doctor.doctor_name);
		holder.level.setText(doctor.unit_name);
		String is_attention = MyApplication.getInstance().doctorMap
				.get(doctor.doctor_id);
		if (is_attention == null || "".equals(is_attention)) {
			is_attention = doctor.is_attention;
		}
		if ("1".equals(is_attention)) {
			holder.attention_layout.setVisibility(View.VISIBLE);
		} else {
			holder.attention_layout.setVisibility(View.INVISIBLE);
		}
	}

	OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			handler.sendMessage(handler.obtainMessage(2, v.getTag()));
		}
	};

	static class ViewHolder {
		LazyImageView image;
		TextView address;
		TextView name;
		TextView level,yuyue_num;
		Button detail;
		ImageView one_icon_img,two_icon_img;
		LinearLayout attention_layout;
	}
}